#include "sct/rtspserver/tsstreamsource.h"

namespace sct
{
using namespace std;

#define N_TS_PACKET_IN_ONE 7
#define TS_PACKET_SIZE 188

CTsStreamSource::CTsStreamSource(CServerInterface * server):
		CStreamSource(server),
		mCnt(0),
		mBuffer(NULL),
		mPts(0)
{
	setName(sct::toString("CTsStreamSource %p", this));
	mBuffer = new uint8_t[N_TS_PACKET_IN_ONE*TS_PACKET_SIZE];
}

CTsStreamSource::~CTsStreamSource()
{
	SCT_DELETE_ARRAY(mBuffer);
}

void CTsStreamSource::onPutData(const void*p, const int len, const int64_t pts)
{
	int left = len;
	uint8_t* pCur = (uint8_t*)p;

	while(left > 0)
	{
		if(0 == mCnt)
		{
			mPts = pts;
		}

		memcpy(mBuffer+mCnt*TS_PACKET_SIZE, pCur, TS_PACKET_SIZE);
		mCnt++;

		if(N_TS_PACKET_IN_ONE == mCnt)
		{
			mCnt = 0;
			CStreamSource::onPutData(mBuffer, N_TS_PACKET_IN_ONE*TS_PACKET_SIZE, mPts);
		}

		pCur += TS_PACKET_SIZE;
		left -= TS_PACKET_SIZE;
	}
}
}
